<?php
declare (strict_types = 1);
namespace app\index\model\common;

use think\Model;

/**
 * @mixin \think\Model
 */
class Advert extends Model
{
    protected $pk = "id";

    /**
     * 获取指定条件的广告
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getListByWhere($where=[],$page=1,$limit=15,$field="*"){
        $sql = $this->where($where)->where('is_status','1')->field($field);
        $count = $sql->count();
        $urlName = config('index.web_url');
        $list = $sql->page($page,$limit)->order('sort','desc')
            ->select()->each(function ($item)use($urlName){
                $item['image'] = $urlName.$item['image'];
            })->toArray();
        return compact('count','list');
    }

    /**
     * 获取指定条件的广告
     * @param array $where
     * @param string $field
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where=[],$field="*"){
        $urlName = config('index.web_url');
        $info = $this->where($where)->where('is_status','1')->field($field)
            ->order('sort','desc')->find();
        if($info) $info['image'] = $urlName.$info['image'];
        return $info;
    }






}
